<template>
  <el-menu :default-active="selectMenu" class="el-menu-vertical-demo" :collapse="isCollapse" background-color="#ffb6c1"
    text-color="#fff" active-text-color="#ff6565" unique-opened>
    <div v-if="!isCollapse" style="display: flex;align-items: center;margin-top: 10px;margin-left:10px;">
      <img src="../assets/images/avatar.png" alt="" width="140px" />
    </div>
    <img v-else src="../assets/images/avatar-1.png" alt="" width="38px" style="margin-left: 12px;margin-top: 10px;" />
    <el-menu-item @click="clickMenu(item)" v-for="item in menu" :key="item.name" :index="item.path">
      <i :class="`el-icon-${item.icon}`"></i>
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>
    <el-submenu v-for="item in hasChildren" :key="item.label" :index="item.label">
      <template slot="title">
        <i :class="`el-icon-${item.icon}`"></i>
        <span slot="title">{{ item.label }}</span>
      </template>
      <el-menu-item-group v-for="subItem in item.children" :key="subItem.path">
        <el-menu-item @click="clickMenu(subItem)" :index="subItem.path">{{ subItem.label }}</el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>
<script>
import Cookie from "js-cookie"
export default {
  data() {
    return {};
  },
  methods: {
    clickMenu(item) {
      if (this.$route.path !== item.path && !(item.path === '/')) {
        this.$router.push(item.path)
      }
      this.$store.commit('selectMenu', item.path)
    }
  },
  computed: {
    menu() {
      return this.menuData.filter(item => !item.children)
    },
    hasChildren() {
      return this.menuData.filter(item => item.children)
    },
    menuData() {
      return JSON.parse(Cookie.get('menu')) || this.$store.state.tab.menu
    },
    selectMenu() {
      return this.$store.state.tab.selectMenu || Cookie.get('selectMenu')
    },
    isCollapse() {
      return this.$store.state.tab.isCollapse
    }
  }
};
</script>
<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.el-menu {
  height: 100vh;
  border-right: none;

  h3 {
    color: #fff;
    text-align: center;
    line-height: 48px;
    font-size: 16px;
    font-weight: 400;
  }
}
</style>